package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐和菜品关系数据
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //自定义方法
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //一基本分页查询
        Page<Setmeal> setmealPage = new Page(page,pageSize);
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        slqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,slqw);

        //完善分页查询
        Page<SetmealDto> dtoPage = new Page<>();
        //数据传输
        BeanUtils.copyProperties(setmealPage,dtoPage,"records");
        //对应的records集合对象
        List<Setmeal> records = setmealPage.getRecords();
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        records.forEach(setmeal -> {
            //创建SetmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //传输数据
            BeanUtils.copyProperties(setmeal,setmealDto);
            //查询categoryName
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            //将数据存入setmealDtos集合
            setmealDtos.add(setmealDto);
        });
        //将集合数据存入分页对象
        dtoPage.setRecords(setmealDtos);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐以及菜品关联数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);

        return R.success("删除成功");
    }

    /**
     * 根据id查询套餐和菜品关系表数据(回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id ){
        SetmealDto setmealDto=setmealService.getWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐和菜品信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 套餐修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        //判断请求状态
        if (status==1){
            //判断菜品是否在售
            //获取套餐菜品关系表数据
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.in(SetmealDish::getSetmealId,ids);
            List<SetmealDish> list = setmealDishService.list(lqw);
            //创建dishId集合
            ArrayList<Long> dishIds = new ArrayList<>();
            list.forEach(setmealDish -> {
                Long dishId = setmealDish.getDishId();
                dishIds.add(dishId);
            });
            //查询dish菜品状态
            LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<Dish>();
            dlqw.in(Dish::getId,dishIds);
            List<Dish> dishs = dishService.list(dlqw);
            for (Dish dish : dishs) {
                if (dish.getStatus()==0){
                    //套餐中有菜品停售状态,套餐无法起售
                    throw new CustomException("套餐中有菜品停售状态,套餐无法起售");
                }
            }

        }
        LambdaUpdateWrapper<Setmeal> slqw = new LambdaUpdateWrapper<>();
        slqw.in(Setmeal::getId,ids);
        slqw.set(Setmeal::getStatus,status);
        setmealService.update(slqw);

        return R.success("修改成功");
    }

    /**
     * 通过分类id和状态查询返回给套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        slqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        slqw.orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(slqw);
        return R.success(list);
    }
}
